﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Business;
using Dto;

namespace Manage.Controllers
{
    public class AdminController : BaseController
    {
        private readonly AdminBusiness _adminBusiness = new AdminBusiness();

        public ActionResult EditAdmin(int? id)
        {
            var adminDto = _adminBusiness.GetAdminById(id ?? 0);
            var roles = _adminBusiness.GetAllRoles();
            ViewData["AdminRole"] = roles;
            return View("~/Views/Admin/EditAdmin.aspx", adminDto ?? new AdminDto());
        }

        public ActionResult AdminList(int? currentPageNo)
        {
            int totalCount;
            var dtos = _adminBusiness.GetAdminList(currentPageNo ?? 1, 10, out totalCount);
            var page = new PagingDto(totalCount, currentPageNo ?? 1, 10, dtos ?? new List<AdminDto>());
            return View("~/Views/Admin/AdminList.aspx", page);
        }

        public ActionResult RoleList()
        {
            List<AdminRoleDto> dtos = _adminBusiness.GetAllRoles();
            return View("~/Views/Admin/RoleList.aspx", dtos);
        }

        public ActionResult RoleManage(int? id)
        {
            var adminModel = Session["Admin"] as AdminDto;
            AdminRoleDto adminRoleModel = null;
            if (adminModel != null)
            {
                int roleID = id ?? 0;
                if (roleID != 0)
                {
                    adminRoleModel = _adminBusiness.GetAdminRole(roleID);
                }
                List<string> roleMenus = null;
                if (adminRoleModel != null && !string.IsNullOrEmpty(adminRoleModel.RoleContent))
                {
                    roleMenus = new List<string>();
                    string[] myMenus = adminRoleModel.RoleContent.Split(new[] {','});
                    foreach (string myMenu in myMenus)
                    {
                        roleMenus.Add(myMenu);
                    }
                }

                ViewData["ManageMenuList"] = _adminBusiness.BuildRoleList(roleMenus,
                                                                          adminModel.RoleDto.RoleContent.Split(new[]
                                                                                                                   {','}));
            }
            return View("~/Views/Admin/RoleManage.aspx", adminRoleModel ?? new AdminRoleDto());
        }

        //public ActionResult SaveAdmin()
        //{

        //}

        public ActionResult SaveRole(string roleName, int roleID, string[] roleContents)
        {
            bool b = false;
            var dto = new AdminRoleDto();
            if (roleID != 0)
            {
                dto = _adminBusiness.GetAdminRole(roleID);
            }

            string[] selectItems = roleContents;
            if (selectItems != null && selectItems.Length > 0)
            {
                for (int i = 0; i < selectItems.Length; i++)
                {
                    string item = selectItems[i];
                    dto.RoleContent += i == 0 ? item : "," + item;
                }

                dto.RoleName = roleName;
                dto.RoleTime = DateTime.Now;
                b = _adminBusiness.SaveRole(dto);
            }
            if (b)
            {
                Response.Write("<script>alert('保存成功！');location.href='" + Url.Action("RoleList") + "';</script>");
                Response.End();
            }
            return RedirectToAction("RoleList");
        }
    }
}